﻿<%@ WebHandler Language="C#" Class="Training.Web.AjaxHandler.Authentication.ForgotPasswordhandler" %>

using System;
using System.Web;


using Newtonsoft.Json.Linq;

using Training.Library.DomainObject.Administration.UserManagement;
using Training.Library.DataAccess.Administration.UserManagement;
//using Training.Library.DomainObject.Administration.Catalog;
//using Training.Library.DataAccess.Administration.Catalog;

using System.Text;
using System.Configuration;


using System.Management;
using System.Management.Instrumentation;
using System.Diagnostics;
using System.Collections.Generic;

using System.Net.Mail;
using System.Net;
using System.Web.SessionState;

using Training.Web;
using Training.Web.Support;

namespace Training.Web.AjaxHandler.Authentication
{
    public class ForgotPasswordhandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);
            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jO = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);
            sr.Close();
            sr.Dispose();
            string message = "Email tidak valid !";
            int isValid = 0;
            try
            {

                string email = (string)jO["email"];
                recUserRepo recUserDal = new recUserRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                recUser user = new recUser();
                isValid = recUserDal.validasiEmail(email);
                if (isValid >0)
                {
                    user = recUserDal.GetByEmail(email);
                    Training.Web.Support.Membership membership = new Training.Web.Support.Membership(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                    string Password = membership.CreatePassword(5);
                    recUserDal.Update(user.UserID,Helper.CreatePasswordHash(Password) , "ForgotPassword",true);
                    MailMessage mail = new MailMessage();
                    MailAddress mailFrom = new MailAddress("no_reply@ojk.go.id", "OJK");
                    mail.From = mailFrom;
                    MailAddress mailTo = new MailAddress(email);
                    mail.To.Add(mailTo);
                    mail.Subject = "OJK - ForgotPassword";   
                    mail.Body = "Masukkan Password Berikut Untuk Login:\n"+Password;
                    SmtpClient smtpEmail = new SmtpClient("mail.ojk.go.id");
                    smtpEmail.EnableSsl = true;
                    smtpEmail.Credentials = new NetworkCredential("no_reply@ojk.go.id", "P4ssword");
                    smtpEmail.Send(mail);
                    
                    message = "Silahkan cek email anda untuk mendapatkan password baru !";
                  //  isValid = 1;
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            finally
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("{");  
                sb.AppendFormat("  {0}Message{0}:{0}{1}{0}", "\"", message);
                sb.Append(",");
                sb.AppendFormat("  {0}IsValid{0}:{0}{1}{0}", "\"", isValid);
                sb.Append("}");
                context.Response.Write(sb.ToString());
            }
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}